package cn.demographic.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import cn.demographic.dao.ApplicanttableMapper;
import cn.demographic.dao.EmploymentMapper;
import cn.demographic.dao.FamilyMapper;
import cn.demographic.dao.LandlordMapper;
import cn.demographic.dao.LiveMapper;
import cn.demographic.dao.LivinginformationMapper;
import cn.demographic.dao.MarriageMapper;
import cn.demographic.dao.SpouseMapper;
import cn.demographic.dao.UserMapper;
import cn.demographic.entity.Applicanttable;
import cn.demographic.entity.Employment;
import cn.demographic.entity.Family;
import cn.demographic.entity.Landlord;
import cn.demographic.entity.Live;
import cn.demographic.entity.Livinginformation;
import cn.demographic.entity.Marriage;
import cn.demographic.entity.Spouse;
import cn.demographic.entity.User;
import cn.demographic.service.UpdateUserSpFaMaEmpLivingService;
/**
 * 修改个人信息
 * 	   配偶信息
 *	   同住家庭
 *	   计生信息
 *	   就业信息
 *	   居住信息
 * @author 
 *
 */
@Service
@Transactional
public class UpdateUserSpFaMaEmpLivingServiceImpl implements UpdateUserSpFaMaEmpLivingService {
	@Resource
	private UserMapper userMapper;
	@Resource
	private LiveMapper liveMapper;
	@Autowired
	private MarriageMapper marriageMapper;
	@Autowired
	private SpouseMapper spouseMapper;
	@Autowired
	private ApplicanttableMapper applicanttableMapper;
	@Autowired
	private FamilyMapper familymapper;
	@Autowired
	private EmploymentMapper employmentMapper;
	@Resource
	private LivinginformationMapper livinginfomationMapper;
	@Resource
	private LandlordMapper landlordMapper;
	@Autowired
	private LivinginformationMapper livinginformationMapper;	

	
	/**
	 * 修改个人信息（流动人口登记信息）
	 * @return
	 */
	public void UpdateUserSpFaMaEmpLiving(Livinginformation livinginformation,
			Landlord landlord, Live liveLive, Integer[] liveid, HttpSession session) {
		/*个人信息*/
		/*添加个人信息*/
		User user = (User) session.getAttribute("user");
		if( user != null ) {
			Live liveUser1 = (Live) session.getAttribute("liveUser");
			liveUser1.setId( liveid[0] );
			Live liveUser2 = (Live) session.getAttribute("liveUser2");
			liveUser2.setId( liveid[1] );
			user.setEnterUser( livinginformation.getInfomation() );
			Integer countUser = updateUser(user,liveUser1,liveUser2);
		}
		/*同住信息*/
		Integer familyUserId = (Integer) session.getAttribute("userid");
		if( familyUserId != null ) {
			String[] withId = (String[]) session.getAttribute("withId");
			String[] betweenId = (String[]) session.getAttribute("betweenId");
			String[] citizenship = (String[]) session.getAttribute("citizenship");
			String[] name = (String[]) session.getAttribute("name");
			String[] gender = (String[]) session.getAttribute("gender");
			String[] birth = (String[]) session.getAttribute("birth");
			String[] certificate = (String[]) session.getAttribute("certificate");
			String[] thisCity = (String[]) session.getAttribute("thisCity");
			Family family = new Family();
			family.setUserid( familyUserId );
			family.setEnterUser( livinginformation.getInfomation() );
			Integer countFamily = updateFamily(withId, betweenId, citizenship, name, gender, birth, certificate, thisCity,family);
		}
		/*计生信息*/
		Marriage marriage = (Marriage) session.getAttribute("record");
		if( marriage !=null ) {
			marriage.setEnterUser( livinginformation.getInfomation() );
			Integer countMarriage = updateMarriage( marriage );
		}
		/*就业信息*/
		Employment employment = (Employment) session.getAttribute("employment");
		if( employment !=null ) {
			Live liveEmployment = (Live) session.getAttribute("liveEmployment");
			liveEmployment.setId( liveid[3] );
			employment.setEnterUser( livinginformation.getInfomation() );
			Integer countEmployment = updateEmployment(employment, liveEmployment);
		}
		/*配偶信息*/
		Spouse spouse = (Spouse) session.getAttribute("spouse");
		if( spouse != null ) {
			Live liveSpouse = (Live) session.getAttribute("liveSpouse");
			liveSpouse.setId( liveid[2] );
			spouse.setEnterUser( livinginformation.getInfomation() );
			Integer countSpouse = updateSpouse(spouse, liveSpouse);
		}
		/*居住信息*/
		liveLive.setId( liveid[4] );
		Integer count = updateLivinginformation(livinginformation,liveLive,landlord);
		/*判断修改是否成功*/
	};
	
	
	/**
	 * 修改个人信息
	 * @param user
	 * @return
	 */
	public int updateUser(User user,Live live,Live live2) {
		/*首先添加地址信息返回地址信息添加的主键ID*/
		Integer livehouseholdId = live.getId();	//户籍
		Integer livenowId = live2.getId();
		Integer userid = user.getId();
		liveMapper.updateLive(live);
		liveMapper.updateLive(live2);
		/*然后在添加个人信息*/
		user.setLivehouseholdid(livehouseholdId);
		user.setLivenowid(livenowId);
		user.setEnterTime( new Date() );
		Integer count = userMapper.updateUser(user);
		return count;
	}

	/**
	 * 修改同住家庭
	 * @param record
	 * @return
	 */
	public int updateFamily(String[] withId, String[] betweenId,
			String[] citizenship,String[] name,
			String[] gender,String[] birth,
			String[] certificate,
			String[] thisCity,
			Family family) {
		Integer[] withIds = new Integer[withId.length];
		for (int i = 0; i < withId.length; i++) {
			if( withId[i] != null && !withId[i].equals( null ) && !StringUtils.isBlank(withId[i]) ) {
				withIds[i] = Integer.parseInt( withId[i] );
			}
		}
		Integer[] betweenIds = new Integer[betweenId.length];
		for (int i = 0; i < betweenId.length; i++) {
			if( betweenId[i] != null && !betweenId[i].equals( null ) && !StringUtils.isBlank(betweenId[i]) ) {
				betweenIds[i] = Integer.parseInt( betweenId[i] );
			}
		}
		Integer[] genders = new Integer[gender.length];
		for (int i = 0; i < gender.length; i++) {
			if( gender[i] != null && !gender[i].equals( null ) && !StringUtils.isBlank(gender[i]) ) {
				genders[i] = Integer.parseInt( gender[i] );
			}
		}
		Integer[] certificates = new Integer[certificate.length];
		for (int i = 0; i < certificate.length; i++) {
			if( certificate[i] != null && !certificate[i].equals( "null" ) && !StringUtils.isBlank(certificate[i]) ) {
				certificates[i] = Integer.valueOf( certificate[i] );
			}
		}
		Integer[] thisCitys = new Integer[thisCity.length];
		for (int i = 0; i < thisCity.length; i++) {
			if( thisCity[i] != null && !thisCity[i].equals( "null" ) && !StringUtils.isBlank(thisCity[i]) ) {
				thisCitys[i] = Integer.parseInt( thisCity[i] );
			}
		}
		Integer count = 0;
		for(int i=0;i<betweenId.length;i++) {
			if( betweenIds[i] != 0 ) {
				family.setNumber(new Integer(i+1));
				family.setBetweenId(betweenIds[i]);
				family.setCitizenship(citizenship[i]);
				family.setName(name[i]);
				family.setGender(genders[i]);
				if( birth[i]!=null && birth[i]!="" ) {
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
					try {
						family.setBirth(dateFormat.parse(birth[i]));
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
				family.setCertificate(certificates[i]);
				family.setThisCity(thisCitys[i]);
				family.setEnterTime(new Date());
				count= familymapper.updateFamily(family);
			}
		}
		return count;
	}

	/**
	 * 修改计生信息
	 * @param record
	 * @return
	 */
	public int updateMarriage(Marriage record) {
		record.setIsdel(0);
		record.setEnterTime(new Date());
		return marriageMapper.updateMarriage(record);
	}

	/**
	 * 修改就业信息
	 * @param record
	 * @return
	 */
	public int updateEmployment(Employment employment, Live live) {
		Integer liveid = live.getId();
		//添加地址信息
		liveMapper.updateLive(live);
		//添加就业信息
		employment.setLiveId( liveid );
		employment.setEnterTime(new Date());
		return employmentMapper.updateEmployment(employment);
	}

	/**
	 * 修改配偶信息
	 * @param record
	 * @return
	 */
	public int updateSpouse(Spouse record,Live live) {
		Integer liveid = live.getId();
		liveMapper.updateLive(live);
		record.setLiveid( liveid );
		record.setEnterTime(new Date());
		return spouseMapper.updateSpouse(record);
	}

	/**
	 * 修改居住信息
	 * @param record
	 * @return
	 */
	public int updateLivinginformation(Livinginformation livinginformation,
			Live live,Landlord landlord) {
		/*添加审核信息*/
		Applicanttable applicanttable = new Applicanttable();
		applicanttable.setUserid( livinginformation.getUserId() );
		applicanttable.setFilltimeid( new Date() );
		applicanttable.setStatus(0);
		applicanttableMapper.updetApplicanttableByid(applicanttable);
		/*修改居住信息*/
		Integer liveid = live.getId();
		liveMapper.updateLive(live);
		Integer landlordid = landlord.getId();
		landlordMapper.updateLandlord(landlord);
		livinginformation.setHouseaddress( liveid );
		livinginformation.setLandlorid( landlordid );
		livinginformation.setEnterTime(new Date());
		return livinginformationMapper.updateLivinginformation(livinginformation);
	}

	/**
	 * 修改地址信息
	 * @param live
	 * @return
	 */
	public int updateLive(Live live) {
		return liveMapper.updateLive(live);
	}
}
